package com.zhangx.datastructure.stacklist;

/**
 * @ClassName ListStack
 * @Description 链栈
 * @Author zhangx
 * @Date 2020/12/31 8:54
 **/
public class LinkStack {

    private StackNode top;
    private int count;

    static class StackNode{
        int data;
        StackNode next;

        @Override
        public String toString() {
            return "StackNode{" +
                    "data=" + data +
                    ", next=" + next +
                    '}';
        }
    }

    void push(int data){
        if (null == top){
            StackNode node = new StackNode();
            node.data = data;
            top = node;

        }else {
            StackNode node = new StackNode();
            node.data = data;
//            新插入的节点在栈顶
            node.next = top;
//            插入的节点成为top
            top = node;
        }
        count++;
    }

    public StackNode pop(){
        StackNode t = top;
        top = top.next;
        count --;
        return t;
    }

    @Override
    public String toString() {
        return "LinkStack{" +
                "top=" + top +
                ", count=" + count +
                '}';
    }



    public static void main(String[] args) {
        LinkStack linkStack = new LinkStack();
        linkStack.push(9);
        System.out.println(linkStack);
        linkStack.push(8);
        System.out.println(linkStack);
        System.out.println(linkStack.pop());
        System.out.println(linkStack);
    }
}
